iT邦幫忙

2024 iThome 鐵人賽

DAY 26
0
Kubernetes

從零到一: 使用Spring Boot、Kubernetes 和 Istio實現微服務架構系列 第 26

Day 26 使用Spring Boot、Kubernetes 和 Istio實現微服務架構 - istio 安全管理

  • 分享至 

  • xImage
  •  

今天來講講 istio 中的安全相關機制,增強一下服務間溝通的安全性

mTLS

傳統的Transport Layer Security (TLS) 是一種常見的加密通訊方式,在TLS通訊中,通常客戶端會驗證伺服器端的身分,並對客戶端與伺服器端的通訊進行加密。而相互驗證mTls,是一種相互驗證的方法,確保兩端都有正確的私鑰可以證明各自的身分,並對客戶端與伺服器端的通訊進行加密。

優點

  • 可以確保雙方身分都是經過認證的,且不會經過未經授權的人訪問與中間人攻擊
  • 減少用戶對於密碼的依賴,降低密碼管理與相關的風險
  • 基於證書訊息可以進行更細粒度的控制

缺點

  • 雙方都需要有證書,因此證書管理的複雜度增加
  • 複雜的配置可能會提高系統的維護成本

總的來說提升服務彼此之間通訊的安全性之餘,會提升整體管理的複雜性,不過isito會負責處理證書管理與相關工作,因此可以在服務規模相對較小時使用,提升整體系統的安全性。

實作

其實當我們安裝istio並將sidecar注入至Pod時,就已經啟動mTLS的功能了,反過來說沒有sidecar的Pod就無法進行mTLS,因此我們使用兩個namespace,分別為isito-sidecar與isito-nosidecar,來進行有sidecar與無sidecar的比較。

環境部屬

  • Deployment(deply.yaml)
apiVersion: v1
kind: Service
metadata:
  name: helloworld-service
spec:
  type: ClusterIP
  selector:
    app: helloworld
  ports:
    - protocol: TCP
      name: http
      port: 5000
      targetPort: 5000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld-v1
  labels:
    app: helloworld
spec:
  replicas: 1
  selector:
    matchLabels:
      app: helloworld
      version: v1
  template:
    metadata:
      labels:
        app: helloworld
        version: v1
    spec:
      containers:
      - name: helloworld-v1
        image: allenku0/hello-api:v1
        ports:
        - containerPort: 5000
        resources:
          limits:
            cpu: 500m
          requests:
            cpu: 200m

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld-v2
  labels:
    app: helloworld
spec:
  replicas: 1
  selector:
    matchLabels:
      app: helloworld
      version: v2
  template:
    metadata:
      labels:
        app: helloworld
        version: v2
    spec:
      containers:
      - name: helloworld-v2
        image: allenku0/hello-api:v2
        ports:
        - containerPort: 5000
        resources:
          limits:
            cpu: 500m
          requests:
            cpu: 200m
  • PeerAuthentication(pa.yaml)
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: mtls-sidecar
  namespace: istio-system
spec:
  mtls:
    mode: STRICT

全域都需要mTLS設定時,指定istio 根namespace來達成全域的設定

  • 部屬
# Deployment
kubectl apply -f deply.yaml -n istio-sidecar
kubectl apply -f deply.yaml -n istio-nodsidecar
# PeerAuthentication
kubectl apply -f pa.yaml
# 在istio安裝的資料夾下
kubectl apply -f samples/sleep/sleep.yaml -n istio-sidecar
kubectl apply -f samples/sleep/sleep.yaml -n istio-nosidecar

結果

  • Sidecar to Sidecar
kubectl exec "$(kubectl get pod -l app=sleep -n istio-sidecar -o jsonpath={.items..metadata.name})" -c sleep -n istio-sidecar -- curl http://helloworld-service.istio-sidecar:5000/api/details -s -o /dev/null -w "%{http_code}\n"
# 200
  • Sidecar to No-Sidecar
kubectl exec "$(kubectl get pod -l app=sleep -n istio-sidecar -o jsonpath={.items..metadata.name})" -c sleep -n istio-sidecar -- curl http://helloworld-service.istio-nosidecar:5000/api/details -s -o /dev/null -w "%{http_code}\n"
# 200
  • No-Sidecar to No Sidecar
kubectl exec "$(kubectl get pod -l app=sleep -n istio-nosidecar -o jsonpath={.items..metadata.name})" -c sleep -n istio-nosidecar -- curl http://helloworld-service.istio-sidecar:5000/api/details -s -o /dev/null -w "%{http_code}\n"
# 000

透過上述方法可以在istio中啟用mTLS,以此方式達到更安全的傳輸
今天就到這邊,我們明天再見!


上一篇
Day 25 使用Spring Boot、Kubernetes 和 Istio實現微服務架構 - istio 流量控制(金絲雀部屬)
系列文
從零到一: 使用Spring Boot、Kubernetes 和 Istio實現微服務架構26
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言